/*
*
*  题目数据模型
*  负责题目部分数据
*
*/
// 导入数据库操作文件
const db = require('../../config/db.js')
module.exports = {
    // 编写添加题目方法
    // 参数：topic_id,topic_title,topic_text,topic_create_time,user_id,topic_navigate,topic_author,topic_option,topic_answer,topic_hot,topic_desc
    add(topic_id, topic_title, topic_text, topic_create_time, user_id, topic_navigate, topic_author, topic_option, topic_answer, topic_hot,topic_desc) {
        return new Promise((resolve, reject) => {
            //处理提交的数据
            let topic_insert_sql = "insert into db_topic(topic_id,topic_title,topic_text,topic_create_time,user_id,topic_navigate,topic_author,topic_option,topic_answer,topic_hot,topic_desc) values('" + topic_id + "','" + topic_title + "','" + topic_text + "','" + topic_create_time + "','" + user_id + "','" + topic_navigate + "','" + topic_author + "','" + topic_option + "','" + topic_answer + "','" + topic_hot + "','"+topic_desc+"')"
            db.query(topic_insert_sql, function (err) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '添加题目失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '添加题目成功。'
                    })
                }
            })
        })
    },

    // 编写删除题目方法
    // 参数：user_id,topic_id
    remove(topic_id) {
        return new Promise((resolve, reject) => {
            db.query("delete from  db_topic where topic_id = '" + topic_id + "'", function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '删除题目失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '删除题目成功。'
                    })
                }
            });
        })
    },

    // 编写获取题目列表方法
    // 参数：无
    list() {
        return new Promise((resolve, reject) => {
            db.query('select * from db_topic', (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取题目列表失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '获取题目列表成功。',
                        result: rows
                    })
                }
            })
        })
    },

     // 编写获取题目列表方法
    // 参数：无
    hot_list() {
        return new Promise((resolve, reject) => {
            db.query('select * from db_topic where topic_hot = "hot"', (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取题目列表失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '获取题目列表成功。',
                        result: rows
                    })
                }
            })
        })
    },

    // 编写搜索题目方法
    // 参数：keyword
    search(keyword) {
        return new Promise((resolve, reject) => {
            db.query("select topic_id,topic_title,topic_navigate,topic_create_time from db_topic where topic_navigate like '%" + keyword + "%' or topic_title like '%" + keyword + "%'", (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询题目失败。',
                        err
                    })
                } else {
                    if (rows.length == 0) {
                        resolve({
                            code: -1,
                            msg: '没有该关键字匹配的题目。'
                        })
                    } else {
                        resolve({
                            code: 200,
                            msg: '查询题目成功。',
                            result: rows
                        })
                    }
                }
            })
        })
    },

    // 编写获取题目详情方法
    // 参数：topic_id
    detail(topic_id) {
        return new Promise((resolve, reject) => {
            // 多字段模糊查询
            db.query("select * from db_topic where topic_id = '" + topic_id + "'", (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询题目失败。',
                        err
                    })
                } else {
                    if (rows.length == 0) {
                        resolve({
                            code: -1,
                            msg: '没有该关键字匹配的题目。'
                        })
                    } else {
                        resolve({
                            code: 200,
                            msg: '查询题目成功。',
                            result: rows
                        })
                    }
                }
            })
        })
    },

    // 编写获取导航题目方法
    // 参数：topic_navigate
    getNavigate(topic_navigate) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_topic where topic_navigate = '" + topic_navigate + "'", (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询导航失败。',
                        err
                    })
                } else {
                    if (rows.length == 0) {
                        resolve({
                            code: 1,
                            msg: '没有改导航分类。'
                        })
                    } else {
                        resolve({
                            code: 200,
                            msg: '查询导航成功。',
                            result: rows
                        })
                    }
                }
            })
        })
    },

    // 编写分页题目列表
    items(page_num, page_size) {
        return new Promise((resolve, reject) => {
            const params = [(parseInt(page_num) - 1) * parseInt(page_size), parseInt(page_size)]
            db.query("select * from db_topic limit " + params[0] + "," + params[1] + "", function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询题目失败。',
                        err
                    })
                } else {
                    //as更换名称
                    db.query('select count(*) as total from db_topic', function (error, among) {
                        if (error) {
                            resolve({
                                code: -1,
                                msg: '统计题目数量失败。',
                                error
                            })
                        } else {
                            resolve({
                                code: 200,
                                msg: "获取分页列表成功。",
                                result: rows,
                                page: {
                                    page_num: page_num,
                                    page_size: page_size,
                                    total: among[0]['total'] //查询表中的数量
                                }
                            })
                        }
                    })
                }
            })

        })
    },

    // 编写分页题目列表(热门)
    hot_items(page_num, page_size) {
        return new Promise((resolve, reject) => {
            const params = [(parseInt(page_num) - 1) * parseInt(page_size), parseInt(page_size)]
            db.query("select * from db_topic limit " + params[0] + "," + params[1] + "", function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询题目失败。',
                        err
                    })
                } else {
                    //as更换名称
                    db.query('select count(*) as total from db_topic', function (error, among) {
                        if (error) {
                            resolve({
                                code: -1,
                                msg: '统计题目数量失败。',
                                error
                            })
                        } else {
                            resolve({
                                code: 200,
                                msg: "获取分页列表成功。",
                                result: rows,
                                page: {
                                    page_num: page_num,
                                    page_size: page_size,
                                    total: among[0]['total'] //查询表中的数量
                                }
                            })
                        }
                    })
                }
            })

        })
    },

    // 编写统计题目数量方法
    all() {
        return new Promise((resolve, reject) => {
            db.query('select count(*) as total from db_topic', function (error, among) {
                if (error) {
                    resolve({
                        code: -1,
                        msg: '统计题目数量失败。',
                        error
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取题目数量成功。",
                        total: among[0]['total']
                    })
                }
            })

        })
    },

    // 编写更新题目方法
    // 参数：topic_id,topic_title,topic_text,topic_create_time,topic_author,topic_navigate,topic_option,topic_answer
    update(topic_id, topic_title, topic_text, topic_create_time, topic_author, topic_navigate, topic_option, topic_answer) {
        return new Promise((resolve, reject) => {
            let update_topic_sql = "update db_topic set topic_title= '" + topic_title + "', topic_text= '" + topic_text + "', topic_create_time= '" + topic_create_time + "', topic_author= '" + topic_author + "', topic_navigate= '" + topic_navigate + "', topic_option= '" + topic_option + "',topic_answer='" + topic_answer + "' where  topic_id= '" + topic_id + "'"
            db.query(update_topic_sql, (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '更新题目失败。', err })
                } else {
                    resolve({ code: 200, msg: '更新题目成功。' })
                }
            })
        })
    },

    // 设置热门题目
    // 编写更新题目方法
    // 参数：topic_id
    hot(topic_id, topic_hot) {
        return new Promise((resolve, reject) => {
            let update_topic_sql = "update db_topic set topic_hot= '" + topic_hot + "' where  topic_id= '" + topic_id + "'"
            db.query(update_topic_sql, (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '推荐热门失败。', err })
                } else {
                    resolve({ code: 200, msg: '推荐热门成功。' })
                }
            })
        })
    },

}